package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedTechParamActual;
import com.sailfish.springbootdemo.pojo.db1.PcbaSpecialRelatedTechParamActual;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaSpecialRelatedTechParamActualDao {

    @Results(id = "pcbaSpecialRelatedTechParamActualResultMap", value = {
            @Result(column = "pcba_device_special_related_tech_param_actual_id", property = "pcbaDeviceSpecialRelatedTechParamActualId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_special_resource_id", property = "pcbaSpecialResourceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_key", property = "techDeviceParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_value", property = "techDeviceParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tech_device_param_value_audit", property = "techDeviceParamValueAudit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    @Select("<script>" +
            " select * from " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL + " where 1 = 1 " +
            "<if test='pcbaSpecialResourceId != null and pcbaSpecialResourceId != \"\" '>" +
            " and pcba_special_resource_id = #{pcbaSpecialResourceId}" +
            "</if>" +
            "</script>")
    List<PcbaSpecialRelatedTechParamActual> getPcbaSpecialRelatedTechParamActualByResourceId(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId);

    @Select("<script>" +
            " select * from " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL + " where 1 = 1 " +
            "<if test='pcbaSpecialResourceId != null and pcbaSpecialResourceId != \"\" '>" +
            " and pcba_special_resource_id = #{pcbaSpecialResourceId}" +
            "</if>" +
            "<if test='techDeviceParamKey != null and techDeviceParamKey != \"\" '>" +
            " and tech_device_param_key = #{techDeviceParamKey}" +
            "</if>" +
            " limit 1" +
            "</script>")
    @ResultMap(value = "pcbaSpecialRelatedTechParamActualResultMap")
    PcbaSpecialRelatedTechParamActual getPcbaSpecialRelatedTechParamActualByResourceIdAndKey(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId, @Param("techDeviceParamKey") String techDeviceParamKey);


    /**
     * 根据设备id和key查询
     *
     * @param techDeviceParamKey
     * @return
     */
    @ResultMap(value = "pcbaSpecialRelatedTechParamActualResultMap")
    @Select("select * from " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL + " t1 left join " + DB1TableName.PCBA_SPECIAL_RESOURCE + " t2 on t1.pcba_special_resource_id = t2.pcba_special_resource_id where t1.pcba_special_resource_id = #{pcbaSpecialResourceId} and t2.pcba_company_id = #{pcbaCompanyId} and t1.tech_device_param_key = #{techDeviceParamKey}")
    PcbaSpecialRelatedTechParamActual getPcbaSpecialRelatedTechParamActualByPcbaDeviceIdAndKey(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId, @Param("techDeviceParamKey") String techDeviceParamKey, @Param("pcbaCompanyId") Integer pcbaCompanyId);

    /**
     * 删除
     *
     * @param pcbaSpecialResourceId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL + " where pcba_special_resource_id = #{pcbaSpecialResourceId}")
    Integer deleteBySpecialResourceId(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId);

    /**
     * 插入
     *
     * @param pcbaSpecialRelatedTechParamActual
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL + "(pcba_special_resource_id,tech_device_param_key,pcba_device_type_id,tech_device_param_value,tech_device_param_value_audit,create_time) " +
            " values (#{pcbaSpecialRelatedTechParamActual.pcbaSpecialResourceId},#{pcbaSpecialRelatedTechParamActual.techDeviceParamKey}," +
            "#{pcbaSpecialRelatedTechParamActual.pcbaDeviceTypeId},#{pcbaSpecialRelatedTechParamActual.techDeviceParamValue},#{pcbaSpecialRelatedTechParamActual.techDeviceParamValueAudit}," +
            "#{pcbaSpecialRelatedTechParamActual.createTime})")
    Integer addPcbaSpecialRelatedTechParamActual(@Param("pcbaSpecialRelatedTechParamActual") PcbaSpecialRelatedTechParamActual pcbaSpecialRelatedTechParamActual);

    /**
     * 更新
     *
     * @param pcbaSpecialRelatedTechParamActual
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL +
            "<set>" +
            "<if test='pcbaSpecialRelatedTechParamActual.pcbaSpecialResourceId != null and pcbaSpecialRelatedTechParamActual.pcbaSpecialResourceId != \"\" '>" +
            " pcba_special_resource_id = #{pcbaSpecialRelatedTechParamActual.pcbaSpecialResourceId}," +
            "</if>" +
            "<if test='pcbaSpecialRelatedTechParamActual.pcbaDeviceParamKey != null and pcbaSpecialRelatedTechParamActual.pcbaDeviceParamKey != \"\" '>" +
            " pcba_device_param_key = #{pcbaSpecialRelatedTechParamActual.pcbaDeviceParamKey}," +
            "</if>" +
            "<if test='pcbaSpecialRelatedTechParamActual.pcbaDeviceTypeId != null and pcbaSpecialRelatedTechParamActual.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaSpecialRelatedTechParamActual.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaSpecialRelatedTechParamActual.techDeviceParamValue != null '>" +
            " tech_device_param_value = #{pcbaSpecialRelatedTechParamActual.techDeviceParamValue}," +
            "</if>" +
            "<if test='pcbaSpecialRelatedTechParamActual.techDeviceParamValueAudit != null '>" +
            " tech_device_param_value_audit = #{pcbaSpecialRelatedTechParamActual.techDeviceParamValueAudit}," +
            "</if>" +
            "<if test='pcbaSpecialRelatedTechParamActual.updateTime != null '>" +
            " update_time = #{pcbaSpecialRelatedTechParamActual.updateTime}," +
            "</if>" +
            "</set>" +
            " where pcba_device_special_related_tech_param_actual_id = #{pcbaSpecialRelatedTechParamActual.pcbaDeviceSpecialRelatedTechParamActualId}" +
            "</script>")
    Integer updatePcbaSpecialRelatedTechParamActual(@Param("pcbaSpecialRelatedTechParamActual") PcbaSpecialRelatedTechParamActual pcbaSpecialRelatedTechParamActual);

    /**
     * 清空对应资源的临时值（审核通过）
     */
    @Update("update " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL + " set tech_device_param_value_audit = '' where pcba_special_resource_id = #{pcbaSpecialResourceId}")
    Integer clearValueAudit(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId);

    /**
     * 将 value 还原成 ValueAudit 的值（审核不通过）
     */
    @Update("update " + DB1TableName.PCBA_SPECIAL_RELATED_TECH_PARAM_ACTUAL + " set tech_device_param_value = tech_device_param_value_audit where pcba_special_resource_id = #{pcbaSpecialResourceId}")
    Integer returnValueFromValueAudit(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId);
}
